eureka简单实例

eureka中,eureka server集群保存客户端信息,客户端获取服务信息、并实现负载均衡。客户端包括:服务调用端(consumer),服务提供端(provider)。

1、eureka server:start.spring.io下新建工程

application配置

server.port = 9090
spring.application.name = eureka.server
#取消服务器自我注册
eureka.client.register-with-eureka=false
eureka.client.fetch-register=false
#用于客户端注册。
eureka.client.serviceUrl.defaultZone= \
  http://localhost:${server.port}/eureka
management.security.enabled = false

引导类ServerApplication

@SpringBootApplication
@EnableEurekaServer
public class ServerApplication {

   public static void main(String[] args) {
      SpringApplication.run(ServerApplication.class, args);
   }

}

2、eureka client:start.spring.io下新建工程

provider application配置

server.port = 8080

spring.application.name = eureka-user-provider
eureka.server.port = 9090

#取消服务器自我注册
#eureka.client.register-with-eureka=false
#eureka.client.fetch-register=false

#用于客户端注册。
eureka.client.serviceUrl.defaultZone= \
  http://localhost:${eureka.server.port}/eureka
management.security.enabled = false

provider端controller通过restTemplate调用

@RestController
public class UserController {
    @Autowired
    UserService userService;

    @PostMapping(path = "/save")
    public boolean save(@RequestBody User user){
        System.out.println("save" + user.toString());
        userService.save(user);
        return true;
    }

    @GetMapping(path = "/list")
    public Collection<User> list(){
        System.out.println("list");
        return userService.list();
    }
}
@SpringBootApplication
@EnableEurekaClient
public class BootStrap {
    public static void main(String[] args) {
        SpringApplication.run(BootStrap.class, args);
    }
}

consumer端application配置

server.port = 7070
spring.application.name = eureka-user-consumer
eureka.server.port = 9090
#取消服务器自我注册
#eureka.client.register-with-eureka=false
#eureka.client.fetch-register=false
#用于客户端注册。
eureka.client.serviceUrl.defaultZone= \
  http://localhost:${eureka.server.port}/eureka
management.security.enabled = false

consumer端通过restTemplate调用provider端controller

@Service
public class UserServiceProxy implements UserService {
    private final String URL_PREX = "http://eureka-user-provider";
    @Autowired
    RestTemplate restTemplate;
    @Override
    public Boolean save(User user) {
        System.out.println("web:save");
        return restTemplate.postForObject(URL_PREX + "/save", user, Boolean.class);
    }

    @Override
    public Collection<User> list() {
        System.out.println("web:list");
        return restTemplate.getForObject(URL_PREX + "/list", Collection.class);
    }
}
@SpringBootApplication
@EnableEurekaClient
public class ConsumerBootStrap {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerBootStrap.class, args);
    }

    @LoadBalanced
    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值