Eureka registry uses

Eureka Registry

stand-alone mode

image-20230306225607857

parent project dependency

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>2021.0.1</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

Then create a rureka project

  • create main class

    annotate @EnableEurekaServer_

  • Configure the yml file

server:
  port: 8888
eureka:
    # 开启之前需要修改一下客户端设置(虽然是服务端
  client:
      # 由于我们是作为服务端角色,所以不需要获取服务端,改为false,默认为true
        fetch-registry: false
        # 暂时不需要将自己也注册到Eureka
    register-with-eureka: false
    # 将eureka服务端指向自己
    service-url:
      defaultZone: http://localhost:8888/eureka
  • Register other services with rereka

    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8888/eureka
    

Now that we have Eureka, we can directly query it to get the corresponding microservice address, here we can directly replace the service name

@Service
public class BorrowServiceImpl implements BorrowService {
    
    

    @Resource
    BorrowMapper mapper;

    @Resource
    RestTemplate template;

    @Override
    public UserBorrowDetail getUserBorrowDetailByUid(int uid) {
    
    
        List<Borrow> borrow = mapper.getBorrowsByUid(uid);

        //这里不用再写IP,直接写服务名称userservice
        User user = template.getForObject("http://userservice/user/"+uid, User.class);
        //这里不用再写IP,直接写服务名称bookservice
        List<Book> bookList = borrow
                .stream()
                .map(b -> template.getForObject("http://bookservice/book/"+b.getBid(), Book.class))
                .collect(Collectors.toList());
        return new UserBorrowDetail(user, bookList);
    }
}

Then we manually declare RestTemplate as a Bean, and then add @LoadBalancedannotations, so that Eureka will automatically discover service calls and provide load balancing:

@Configuration
public class BeanConfig {
    
    
    @Bean
    @LoadBalanced
    RestTemplate template(){
    
    
        return new RestTemplate();
    }
}

Eureka cluster

First we need to modify the configuration file of the Eureka server, here we create two configuration files

server:
  port: 8801
spring:
  application:
    name: eurekaserver
eureka:
  instance:
      # 由于不支持多个localhost的Eureka服务器,但是又只有本地测试环境,所以就只能自定义主机名称了
      # 主机名称改为eureka01
    hostname: eureka01
  client:
    fetch-registry: false
    # 去掉register-with-eureka选项,让Eureka服务器自己注册到其他Eureka服务器,这样才能相互启用
    service-url:
        # 注意这里填写其他Eureka服务器的地址,不用写自己的
      defaultZone: http://eureka01:8802/eureka

Here, because we modify it to a custom address, we need to resolve it to 172.0.0.1 in the hosts file to return to localhost. Under Windows, it isC:\Windows\System32\drivers\etc\hosts

image-20230324100050055

image-20230324102158183

Then we need to modify our microservices, otherwise the rureka cluster cannot be registered

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8801/eureka, http://localhost:8801/eureka

There is also an annotated microservice address

OpenFeign implements load balancing

Official documentation: https://docs.spring.io/spring-cloud-openfeign/docs/current/reference/html/

Feign, like RestTemplate, is also an HTTP client request tool, but it is more convenient to use. First is the dependency:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

Then add @EnableFeignClientsannotations to the startup class:

@SpringBootApplication
@EnableFeignClients
public class BorrowApplication {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(BorrowApplication.class, args);
    }
}

So now we need to call the interfaces provided by other microservices, what should we do? We can directly create an interface class corresponding to the service:

@FeignClient("userservice")   //声明为userservice服务的HTTP请求客户端
public interface UserClient {
    
    
}

Then we directly create the method of the required type, such as our previous:

RestTemplate template = new RestTemplate();
User user = template.getForObject("http://userservice/user/"+uid, User.class);

It can now be written directly like this:

@FeignClient("userservice")
public interface UserClient {
    
    

      //路径保证和其他微服务提供的一致即可
    @RequestMapping("/user/{uid}")
    User getUserById(@PathVariable("uid") int uid);  //参数和返回值也保持一致
}

Then you can make requests like mabatis

@Resource
UserClient userClient;

@Override
public UserBorrowDetail getUserBorrowDetailByUid(int uid) {
    
    
    List<Borrow> borrow = mapper.getBorrowsByUid(uid);
    
    User user = userClient.getUserById(uid);
    //这里不用再写IP,直接写服务名称bookservice
    List<Book> bookList = borrow
            .stream()
            .map(b -> template.getForObject("http://bookservice/book/"+b.getBid(), Book.class))
            .collect(Collectors.toList());
    return new UserBorrowDetail(user, bookList);
}

Guess you like

Origin blog.csdn.net/m0_57647880/article/details/131114899