资讯专栏INFORMATION COLUMN

分布式-SpringBoot-SpringCloud-Eureka

Olivia / 2918人阅读

摘要:环境搭建注册中心依赖如下所示配置应用启动端口注册中心管理中的应用名称登陆注册管理中的的账号密码是否把自己注册到注册中心是否从上来获取服务的注册信息启动注册中心启动后访问登陆界面输入设置的账号密码进

</>复制代码

  1. 环境

Java version 1.8

SpringBoot version 2.1.7

</>复制代码

  1. 搭建注册中心 Eureka-server

pom.xml 依赖如下所示:

</>复制代码

  1. org.springframework.cloud
  2. spring-cloud-starter-netflix-eureka-server
  3. org.springframework.boot
  4. spring-boot-starter-test
  5. test
  6. org.springframework.boot
  7. spring-boot-starter-security
  8. org.springframework.boot
  9. spring-boot-starter-security
  10. javax.xml.bind
  11. jaxb-api
  12. com.sun.xml.bind
  13. jaxb-impl
  14. 2.3.0
  15. org.glassfish.jaxb
  16. jaxb-runtime
  17. 2.3.0
  18. javax.activation
  19. activation
  20. 1.1.1

配置 Eureka application.properties

</>复制代码

  1. # 应用启动端口
  2. server.port=8090
  3. # 注册中心管理中的 应用名称
  4. spring.application.name=eureka-server
  5. # 登陆注册管理中的的账号密码
  6. spring.security.user.roles=SUPERUSER
  7. spring.security.user.name=eureka
  8. spring.security.user.password=123456
  9. # 是否把自己注册到注册中心
  10. eureka.client.register-with-eureka=true
  11. # 是否从eureka上来获取服务的注册信息
  12. eureka.client.fetch-registry=false
  13. eureka.instance.hostname=localhost
  14. eureka.client.serviceUrl.defaultZone=http://eureka:123456@localhost:8090/eureka

启动注册中心 启动后访问(http://127.0.0.1:8090)

登陆界面

</>复制代码

  1. 2. 输入设置的账号密码(user:eureka pwd:123456)
  2. 3. 进入注册中心页面

需要注意的是 需要处理下CSRF不然服务提供者注册不进来 启动入口文件如下

EurekaServiceApplication.java

</>复制代码

  1. @EnableEurekaServer
  2. @SpringBootApplication
  3. public class EurekaServiceApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(EurekaServiceApplication.class, args);
  6. }
  7. /**
  8. * 忽略 uri /eureka/** 的CSRF检测
  9. */
  10. @EnableWebSecurity
  11. static class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  12. @Override
  13. protected void configure(HttpSecurity http) throws Exception {
  14. http.csrf().ignoringAntMatchers("/eureka/**");
  15. super.configure(http);
  16. }
  17. }
  18. }

简单的注册中心已经搭建完成

</>复制代码

  1. 搭建服务提供者 Provider-service

首先还是配置文件 application.properties

</>复制代码

  1. 服务端口
  2. server.port=8081
  3. # 服务应用名称
  4. spring.application.name=provider-ticket
  5. # 是否允许使用ip连接
  6. eureka.instance.ip-address=true
  7. # 注意 http://用户名:密码@主机:端口/eureka 需要与服务中心里配置的一样
  8. eureka.client.serviceUrl.defaultZone=http://eureka:123456@localhost:8090/eureka

启动 项目后会自动把服务注册到配置的服务中心

以下是我做的测试代码

TicketController.java

</>复制代码

  1. package com.yourdream.providerticket.controller;
  2. import com.yourdream.providerticket.service.TicketService;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. @RestController
  7. public class TicketController {
  8. @Autowired
  9. TicketService ticketService;
  10. @GetMapping("/ticket")
  11. public String getTicket(){
  12. System.out.println("server-8081");
  13. return ticketService.getTicket();
  14. }
  15. }

TicketService

</>复制代码

  1. package com.yourdream.providerticket.service;
  2. import org.springframework.stereotype.Service;
  3. @Service
  4. public class TicketService {
  5. public String getTicket()
  6. {
  7. return "《哪吒之魔童降世》";
  8. }
  9. }

ProviderTicketApplication

</>复制代码

  1. package com.yourdream.providerticket;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class ProviderTicketApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(ProviderTicketApplication.class, args);
  8. }
  9. }

到此一个简单的服务提供者搭建完成

</>复制代码

  1. 搭建服务消费者 Consumer-user

首先还是配置文件 application.properties

</>复制代码

  1. server.port=8100
  2. spring.application.name=consumer-user
  3. eureka.client.serviceUrl.defaultZone=http://eureka:123456@localhost:8090/eureka
  4. eureka.instance.ip-address=true

首先需要明确 消费者与服务提供者之间通讯协议是HTTP

看下实例代码

启动入口 ConsumerUserApplication.java

</>复制代码

  1. package com.yourdream.consumeruser;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.web.client.RestTemplate;
  8. @EnableDiscoveryClient //开启发现服务功能
  9. @SpringBootApplication
  10. public class ConsumerUserApplication {
  11. public static void main(String[] args) {
  12. SpringApplication.run(ConsumerUserApplication.class, args);
  13. }
  14. @LoadBalanced //开启负载均衡器
  15. @Bean //注册 RestTemplate 服务
  16. public RestTemplate restTemplate()
  17. {
  18. return new RestTemplate();
  19. }
  20. }

UserController.java

</>复制代码

  1. package com.yourdream.consumeruser.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. import org.springframework.web.client.RestTemplate;
  6. @RestController
  7. public class UserController {
  8. // 帮助我们向 注册的实例服务 发起http请求
  9. @Autowired
  10. RestTemplate restTemplate;
  11. @GetMapping("/buy")
  12. public String buyTicket(String name)
  13. {
  14. //PROVIDER-TICKET 服务提供者名称
  15. String forObject = restTemplate.getForObject("http://PROVIDER-TICKET/ticket", String.class);
  16. return name + "购买了" + forObject;
  17. }
  18. }

启动服务 访问http://127.0.0.1:8100/buy

到此一个简单的服务消费者搭建完成

</>复制代码

  1. 原文

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/76261.html

相关文章

  • 布式ID系列(1)——为什么需要布式ID以及布式ID的业务需求

    摘要:同时除了对号码自身的要求,业务还对号生成系统的可用性要求极高,想象一下,如果生成系统瘫痪,整个美团点评支付优惠券发券骑手派单等关键动作都无法执行,这就会带来一场灾难。 分布式id主要用到哪些地方 在复杂分布式系统中,往往需要对大量的数据和消息进行唯一标识。如在美团点评的金融、支付、餐饮、酒店、猫眼电影等产品的系统中,数据日渐增长,对数据分库分表后需要有一个唯一ID来标识一条数据或消息,...

    Snailclimb 评论0 收藏0
  • 布式数据库火了 开源填补数据库空白

    摘要:利用分布式数据库实现了物理分离,逻辑统一的优势。这样的基本特点让分布式数据库具有数据独立性和位置透明性,局部应用响应速度升级。在互联网大潮的背景下,中国的分布式数据库出现,这也填补了国内数据库的空白。 原文地址:http://digi.163.com/17/1114/0... 分布式数据库在今年突然成为热点技术。这背后究竟有哪些环境变化导致了这种技术演进,分布式数据库的优势在哪儿?这种...

    wqj97 评论0 收藏0

发表评论

0条评论

Olivia

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<