Here service provider and a consumer-based Spring Boot develop a service. No service discovery component, direct service consumers to access services provided by the external service provider through URL.
- Service providers to provide services via REST interface.
- Services API consumers who RestTemplate by calling the service.
A. Service provider code
Create a project simple-provider-user use Spring Boot.
- Use Spring Data JPA as persistence framework;
- Using H2 as the database.
The introduction of dependence:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
Prepare data schema.sql:
drop table user if exists; create table user ( id bigint generated by default as identity, username varchar(40), name varchar(20), age int(3), balance decimal(10,2), primary key (id) ); insert into user (id, username, name, age, balance) values (1, 'account1', '张三', 20, 100.00); insert into user (id, username, name, age, balance) values (2, 'account2', '李四', 28, 180.00); insert into user (id, username, name, age, balance) values (3, ' Account3 ' , ' Wang Wu ' , 32 , 280.00 );
Entity class User:
@Entity public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Column private String username; @Column private String name; @Column private Integer age; @Column private BigDecimal balance; // setters getters }
Creating DAO:
@Repository public interface UserRepository extends JpaRepository<User, Long> { }
Creating Controller:
@RestController public class UserController { @Autowired private UserRepository userRepository; @GetMapping("/{id}") public User findById(@PathVariable Long id) { User user = userRepository.findOne(id); return user; } }
Write a configuration file application.yml:
server: port: 8000 spring: jpa: generate-ddl: false show-sql: true hibernate: bill - car: none datasource: platform: h2 logging: level: root: INFO org.hibernate: INFO
test. Access HTTP: // localhost: 8000/1 , get results:
{ "id": 1, "username": "account1", "name": "张三", "age": 20, "balance": 100 }
II. Services Consumer Code
Creating a web starter as project lead use Spring Boot.
Add RestTemplate Bean:
@SpringBootApplication public class SimpleConsumerMovieApplication { public static void main(String[] args) { SpringApplication.run(SimpleConsumerMovieApplication.class, args); } @Bean public Residual Template rest template () { return new Rest Template (); } }
RestTemplate API access service providers by:
@RestController public class MovieController { @Autowired private Rest Template rest template; @GetMapping("/user/{id}") public User findById(@PathVariable Long id) { String url = "http://localhost:8000/" + id; return restTemplate.getForObject(url, User.class); } }
Configuration service consumers start port:
server: port: 8010
test:
Respectively, to start the service provider processes and services consumers. Access: localhost: 8010 / user / 1, the following results:
{ "id": 1, "username": "account1", "name": "张三", "age": 20, "balance": 100 }
III. Summary
In this example only the service providers and service consumers two roles, without introducing service discovery component, there are some drawbacks:
When we access the service provider API is the network address hard-coded service provider in the code (even if the preparation can be considered in the configuration file).
If the service provider's network address changes, we need to modify the service consumer code, and then republish.
In order to build a good micro-service system, we need to introduce service discovery component, so that service providers can register their information to the service provided by the service discovery component, the service can be found by service consumers set up to get the information needed to access the service and then go to access related services.