共计 5004 个字符,预计需要花费 13 分钟才能阅读完成。
环境
-
Java version 1.8
-
SpringBoot version 2.1.7
搭建注册中心 Eureka-server
- pom.xml 依赖如下所示:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
- 配置 Eureka application.properties
# 应用启动端口
server.port=8090
# 注册中心管理中的 应用名称
spring.application.name=eureka-server
# 登陆注册管理中的的账号密码
spring.security.user.roles=SUPERUSER
spring.security.user.name=eureka
spring.security.user.password=123456
# 是否把自己注册到注册中心
eureka.client.register-with-eureka=true
# 是否从 eureka 上来获取服务的注册信息
eureka.client.fetch-registry=false
eureka.instance.hostname=localhost
eureka.client.serviceUrl.defaultZone=http://eureka:123456@localhost:8090/eureka
-
启动注册中心 启动后访问(http://127.0.0.1:8090)
- 登陆界面
2. 输入设置的账号密码(user:eureka pwd:123456)3. 进入注册中心页面
- 需要注意的是 需要处理下 CSRF 不然服务提供者注册不进来 启动入口文件如下
- EurekaServiceApplication.java
@EnableEurekaServer
@SpringBootApplication
public class EurekaServiceApplication {public static void main(String[] args) {SpringApplication.run(EurekaServiceApplication.class, args);
}
/**
* 忽略 uri /eureka/** 的 CSRF 检测
*/
@EnableWebSecurity
static class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {http.csrf().ignoringAntMatchers("/eureka/**");
super.configure(http);
}
}
}
- 简单的注册中心已经搭建完成
搭建服务提供者 Provider-service
- 首先还是配置文件 application.properties
服务端口
server.port=8081
# 服务应用名称
spring.application.name=provider-ticket
# 是否允许使用 ip 连接
eureka.instance.ip-address=true
# 注意 http:// 用户名: 密码 @主机: 端口 /eureka 需要与服务中心里配置的一样
eureka.client.serviceUrl.defaultZone=http://eureka:123456@localhost:8090/eureka
- 启动 项目后会自动把服务注册到配置的服务中心
- 以下是我做的测试代码
- TicketController.java
package com.yourdream.providerticket.controller;
import com.yourdream.providerticket.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TicketController {
@Autowired
TicketService ticketService;
@GetMapping("/ticket")
public String getTicket(){System.out.println("server-8081");
return ticketService.getTicket();}
}
- TicketService
package com.yourdream.providerticket.service;
import org.springframework.stereotype.Service;
@Service
public class TicketService {public String getTicket()
{return "《哪吒之魔童降世》";}
}
- ProviderTicketApplication
package com.yourdream.providerticket;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ProviderTicketApplication {public static void main(String[] args) {SpringApplication.run(ProviderTicketApplication.class, args);
}
}
- 到此一个简单的服务提供者搭建完成
搭建服务消费者 Consumer-user
- 首先还是配置文件 application.properties
server.port=8100
spring.application.name=consumer-user
eureka.client.serviceUrl.defaultZone=http://eureka:123456@localhost:8090/eureka
eureka.instance.ip-address=true
- 首先需要明确 消费者与服务提供者之间通讯协议是HTTP
- 看下实例代码
- 启动入口 ConsumerUserApplication.java
package com.yourdream.consumeruser;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@EnableDiscoveryClient // 开启发现服务功能
@SpringBootApplication
public class ConsumerUserApplication {public static void main(String[] args) {SpringApplication.run(ConsumerUserApplication.class, args);
}
@LoadBalanced // 开启负载均衡器
@Bean // 注册 RestTemplate 服务
public RestTemplate restTemplate()
{return new RestTemplate();
}
}
- UserController.java
package com.yourdream.consumeruser.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class UserController {
// 帮助我们向 注册的实例服务 发起 http 请求
@Autowired
RestTemplate restTemplate;
@GetMapping("/buy")
public String buyTicket(String name)
{
//PROVIDER-TICKET 服务提供者名称
String forObject = restTemplate.getForObject("http://PROVIDER-TICKET/ticket", String.class);
return name + "购买了" + forObject;
}
}
- 启动服务 访问 http://127.0.0.1:8100/buy
- 到此一个简单的服务消费者搭建完成
正文完
发表至: java
2019-08-17